package com.glsc.ngateway.oaflow.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.glsc.ngateway.oaflow.utils.Constant.PRIMARY_NODE;
import static com.glsc.ngateway.oaflow.utils.Constant.SECOND_NODE;


@Component
public class OracleJdbcTemplate {
    Logger logger = LoggerFactory.getLogger(OracleJdbcTemplate.class);

    private JdbcTemplate jdbcTemplateOracle;
    private JdbcTemplate jdbcTemplateOracleSecond;

    @Autowired
    @Qualifier("datasourceOracle")
    private DataSource datasourceOracle;  // 直接注入 routingDataSource

    @Autowired
    @Qualifier("datasourceOracleSecond")
    private DataSource datasourceOracleSecond;

    private JdbcTemplate jdbcTemplate;

    @Resource
    private SwitchDataSourceService switchDataSourceService;

    String currentJdbc;

    public String getCurrentJdbc() {
        return currentJdbc;
    }

    public void setCurrentJdbc(String currentJdbc) {
        this.currentJdbc = currentJdbc;
    }

    @PostConstruct
    public void init() {
        this.jdbcTemplateOracle = new JdbcTemplate(datasourceOracle);
        this.jdbcTemplateOracleSecond = new JdbcTemplate(datasourceOracleSecond);
        jdbcTemplate = jdbcTemplateOracle;
        currentJdbc = PRIMARY_NODE;
    }

    public void switchJdbc(){
        logger.info("switch Node"+ currentJdbc);
        if (StrUtil.equals(currentJdbc,PRIMARY_NODE)){
            jdbcTemplate = jdbcTemplateOracleSecond;
            currentJdbc = SECOND_NODE;
        } else {
            jdbcTemplate = jdbcTemplateOracle;
            currentJdbc = PRIMARY_NODE;
        }
        switchDataSourceService.switchSource();
        logger.info("switch Success"+ currentJdbc);
    }

    public List<Map<String, Object>> query(String sql) {
        List<Map<String, Object>> ret = new ArrayList<>();
        try {
            ret = jdbcTemplate.queryForList(sql);
        } catch (Exception e){
            e.printStackTrace();
            switchJdbc();
            ret = jdbcTemplate.queryForList(sql);
        }
        return ret;
    }

    public List<Map<String, Object>> queryPre(String sql,Object[] args) {
        logger.info("sql"+sql+"--args"+ JSONObject.toJSONString(args));
        List<Map<String, Object>> ret = new ArrayList<>();
        try {
            ret = jdbcTemplate.queryForList(sql,args);
        } catch (Exception e){
            ret = jdbcTemplate.queryForList(sql,args);
        }
        return ret;
    }

    public String queryString(String sql) {
        List<String> strings = new ArrayList<>();
        try{
            strings = jdbcTemplate.queryForList(sql, String.class);
        }catch (Exception e){
            e.printStackTrace();
            strings = jdbcTemplate.queryForList(sql, String.class);
        }
        if(CollectionUtil.isEmpty(strings)){
            return null;
        }
        logger.info("sql  结果: " + CollectionUtil.join(strings,","));
        return strings.get(0);
    }

}
